﻿using System;
using System.Threading;
using System.Timers;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Media3D;
using AutoLoL.Entities;
using AutoLoL.Logic.Utils;
using System.Collections.Generic;
using AutoLoL.Logic.Parsers;
using AutoLoL.Logic.Factories;

namespace AutoLoL.Tabs
{
    /// <summary>
    /// Interaction logic for ModelTab.xaml
    /// </summary>
    public partial class ModelTab : UserControl
    {
        private System.Timers.Timer _filterTimer;

        public ModelTab()
        {
            InitializeComponent();

            ChampionSelectControl.IsMultipleSelectAllowed(false);

            _filterTimer = new System.Timers.Timer(100);
            _filterTimer.Elapsed += new ElapsedEventHandler(FilterTimer_Elapsed);
            _filterTimer.AutoReset = false;
            _filterTimer.Enabled = true;
            _filterTimer.Stop();
        }

        private void ChampionSelectControl_SelectionChanged(object sender, Controls.SelectionChangedArgs e)
        {
            Champion champion = e.Champions[0];

            if (champion.Skins == null)
            {
                ChampionFactory.Instance.SetSkins(champion);
            }

            SkinSelectListBox.ItemsSource = champion.Skins;

            if (champion.Skins.Count > 0)
            {
                SkinSelectListBox.SelectedIndex = 0;
            }
        }

        private void SkinSelectListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                Skin skin = e.AddedItems[0] as Skin;
                if (skin != null)
                {
                    RafManager.Instance.SetRafFileData(skin.DdsRafFile);
                    RafManager.Instance.SetRafFileData(skin.SknRafFile);

                    byte[] ddsData = skin.DdsRafFile.Data;
                    byte[] sknData = skin.SknRafFile.Data;

                    SknFileNameTextBlock.Text = System.IO.Path.GetFileNameWithoutExtension(skin.SknFileName);
                    DdsFileNameTextBlock.Text = System.IO.Path.GetFileNameWithoutExtension(skin.DdsFileName);

                    Model3D model = ModelConverter.GetFromSkin(skin);
                    HelixExpander3D.Content = model;
                    HelixView.ZoomToFit(0);
                }
            }
        }

        private void SetFilter()
        {
            string[] filters = FilterTextBox.Text.ToLower().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            ChampionSelectControl.SetFilter(filters);
        }

        private void FilterTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                SetFilter();
            }
        }

        private void FilterTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Dispatcher.BeginInvoke((ThreadStart)(() =>
            {
                SetFilter();
            }));
        }

        private void FilterTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            _filterTimer.Stop();
            _filterTimer.Start();
        }
    }
}
